﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.Data;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// EmailFuncs work with sending email in various ways.
/// </summary>
public static class EmailFuncs
{

    public static bool SendPasswordExpirationNotices()
    {
        ADFuncs ADQuery = new ADFuncs();
        DataTable AlmostExpiredAccounts = ADQuery.GetUsersWithPasswordsExpiring(30);
        string Subject;
        string MessageBody;

        foreach(DataRow TempRow in AlmostExpiredAccounts.Rows)
        {
            if(Convert.ToDateTime(TempRow["DateExpires"].ToString()).Subtract(DateTime.Today).Days == 30)
            {
                Subject = "Network Password Expiring";
                MessageBody = "<p>Greetings.  <br>"
                    + "The RVA network password for the user named " + TempRow["Username"].ToString() + " will expire in 30 days. <br />"
                    + "If you would like to change it immediately, <a href='https://portal.rva.org/Account/LoginNR'>click here</a><br />"
                    + "<b>Remember that this password is used for mapped drives, network logins, and logging into the "
                    + "RVA Content Filter (outside.rva.org).</b>"  ;
                BasicFuncs.SendEmail(Subject, MessageBody, TempRow["Username"].ToString() + "@rva.org");
            }
            else if (Convert.ToDateTime(TempRow["DateExpires"].ToString()).Subtract(DateTime.Today).Days < 0)
            {
                Subject = "Network Password Expired";
                MessageBody = "<p>Greetings.  <br>"
                    + "The RVA network password for the user named " + TempRow["Username"].ToString() + " has expired. <br />"
                    + "If you would like to change it immediately, <a href='https://portal.rva.org/Account/LoginNR'>click here</a><br />"
                    + "<b>Remember that this password is used for mapped drives, network logins, and logging into the "
                    + "RVA Content Filter (outside.rva.org).</b>";
                BasicFuncs.SendEmail(Subject, MessageBody, TempRow["Username"].ToString() + "@rva.org");
            }
            else if (Convert.ToDateTime(TempRow["DateExpires"].ToString()).Subtract(DateTime.Today).Days <= 7)
            {
                Subject = "Network Password Expiring";
                MessageBody = "<p>Greetings.  <br>"
                    + "The RVA network password for the user named " + TempRow["Username"].ToString() + " will expire in " + Convert.ToDateTime(TempRow["DateExpires"].ToString()).Subtract(DateTime.Today).Days
                    + " days. <br />"
                    + "If you would like to change it immediately, <a href='https://portal.rva.org/Account/LoginNR'>click here</a><br />"
                    + "<b>Remember that this password is used for mapped drives, network logins, and logging into the "
                    + "RVA Content Filter (outside.rva.org).</b>";
                BasicFuncs.SendEmail(Subject, MessageBody, TempRow["Username"].ToString() + "@rva.org");
            }
        }
        return true;
    }
}